package byx.container.core.utils;

import byx.container.core.ObjectFactory;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class TypeRef<T> {
    private final ObjectFactory<?> factory;

    protected TypeRef(ObjectFactory<T> factory) {
        this.factory = factory;
    }

    public ObjectFactory<?> getFactory() {
        return factory;
    }

    public Class<?> getGenericType() {
        ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
        Type typeArgument = genericSuperclass.getActualTypeArguments()[0];
        if (typeArgument instanceof ParameterizedType) {
            return (Class<?>) ((ParameterizedType) typeArgument).getRawType();
        } else {
            return (Class<?>) typeArgument;
        }
    }
}
